package com.yiwei.hdb.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.util.UI;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class CodeVerifyActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.view_pass_one)
    View viewPassOne;
    @BindView(R.id.view_pass_one_bottom)
    View viewPassOneBottom;
    @BindView(R.id.txt_pass_one)
    TextView txtPassOne;
    @BindView(R.id.view_pass_two)
    View viewPassTwo;
    @BindView(R.id.view_pass_two_bottom)
    View viewPassTwoBottom;
    @BindView(R.id.txt_pass_two)
    TextView txtPassTwo;
    @BindView(R.id.view_pass_three)
    View viewPassThree;
    @BindView(R.id.view_pass_three_bottom)
    View viewPassThreeBottom;
    @BindView(R.id.txt_pass_three)
    TextView txtPassThree;
    @BindView(R.id.view_pass_four)
    View viewPassFour;
    @BindView(R.id.view_pass_four_bottom)
    View viewPassFourBottom;
    @BindView(R.id.txt_pass_four)
    TextView txtPassFour;
    @BindView(R.id.edit_code)
    EditText editCode;
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_verify_code);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        editCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = UI.toString(editCode);
                initStatus();
                switch (text.length()) {
                    case 0:

                        break;
                    case 1:
                        txtPassOne.setText(text.charAt(0) + "");
                        viewPassTwo.setVisibility(View.VISIBLE);
                        viewPassTwoBottom.setBackgroundColor(0xff3C7BFC);
                        break;
                    case 2:
                        txtPassOne.setText(text.charAt(0) + "");
                        txtPassTwo.setText(text.charAt(1) + "");
                        viewPassThree.setVisibility(View.VISIBLE);
                        viewPassThreeBottom.setBackgroundColor(0xff3C7BFC);
                        break;
                    case 3:
                        txtPassOne.setText(text.charAt(0) + "");
                        txtPassTwo.setText(text.charAt(1) + "");
                        txtPassThree.setText(text.charAt(2) + "");
                        viewPassFour.setVisibility(View.VISIBLE);
                        viewPassFourBottom.setBackgroundColor(0xff3C7BFC);
                        break;
                    case 4:
                        txtPassOne.setText(text.charAt(0) + "");
                        txtPassTwo.setText(text.charAt(1) + "");
                        txtPassThree.setText(text.charAt(2) + "");
                        txtPassFour.setText(text.charAt(3) + "");
                        hideInput();
                        break;
                }
            }
        });
    }

    private void initStatus() {
        txtPassOne.setText("");
        txtPassTwo.setText("");
        txtPassThree.setText("");
        txtPassFour.setText("");
        viewPassOne.setVisibility(View.GONE);
        viewPassTwo.setVisibility(View.GONE);
        viewPassThree.setVisibility(View.GONE);
        viewPassFour.setVisibility(View.GONE);
        viewPassOneBottom.setBackgroundColor(0xffDEDEDE);
        viewPassTwoBottom.setBackgroundColor(0xffDEDEDE);
        viewPassThreeBottom.setBackgroundColor(0xffDEDEDE);
        viewPassFourBottom.setBackgroundColor(0xffDEDEDE);
    }

    private void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editCode.getWindowToken(), 0); //强制隐藏键盘
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }
}
